package com.java.Tomcat;

import java.io.*;

public class MyHttpServletResponse {
    private MyHttpServletRequest request;
    private OutputStream outputStream;

    public MyHttpServletResponse(MyHttpServletRequest request, OutputStream outputStream) {
        this.request = request;
        this.outputStream = outputStream;

        // 读取请求文件内容并拼接协议响应
        outputHttpResponse();
    }

    private void outputHttpResponse() {
        String requestURI = this.request.getRequestURI();
        File file = new File(this.request.getRealPath() + requestURI);

        byte[] fileContentBytes = null;
        String responseProtocol = null;
        if (!file.exists()) {
            fileContentBytes = readFileContentToBytes(new File(this.request.getRealPath(), "404.html"));
            responseProtocol = get404Protocol(fileContentBytes); // 生成404协议
        } else {
            fileContentBytes = readFileContentToBytes(file);
            responseProtocol = get200Protocol(fileContentBytes); // 生成200协议
        }

        try {
            this.outputStream.write(responseProtocol.getBytes());
            this.outputStream.flush();
            this.outputStream.write(fileContentBytes);
            this.outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private byte[] readFileContentToBytes(File file) {
        //字节数组内存流，用于数据很大情况
        //ByteArrayOutputStream写在内存上使用
        try (
                FileInputStream fileInputStream = new FileInputStream(file);
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ) {
            int length = -1;
            byte[] buffer = new byte[10 * 1024];
            while ((length = fileInputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, length);
                byteArrayOutputStream.flush();
            }
            return byteArrayOutputStream.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private String get200Protocol(byte[] fileContentBytes) {
        String requestURI = this.request.getRequestURI();
        int index = requestURI.lastIndexOf(".");
        if (index == -1) {
            index = index + 1;
        }
        String extension = requestURI.substring(index + 1);

        String pro200 = "";
        if ("html".equalsIgnoreCase(extension) || "htm".equalsIgnoreCase(extension)) {
            pro200 = "HTTP/1.1 200\r\n" +
                    "Content-Type: text/html\r\n" +
                    "Content-Length: " + fileContentBytes.length + "\r\n\r\n";
        } else if ("css".equalsIgnoreCase(extension)) {
            pro200 = "HTTP/1.1 200\r\n" +
                    "Content-Type: text/css\r\n" +
                    "Content-Length: " + fileContentBytes.length + "\r\n\r\n";
        } else if ("js".equalsIgnoreCase(extension)) {
            pro200 = "HTTP/1.1 200\r\n" +
                    "Content-Type: text/javascript\r\n" +
                    "Content-Length: " + fileContentBytes.length + "\r\n\r\n";
        } else if ("jpg".equalsIgnoreCase(extension) || "jpeg".equalsIgnoreCase(extension)) {
            pro200 = "HTTP/1.1 200\r\n" +
                    "Content-Type: text/jpeg\r\n" +
                    "Content-Length: " + fileContentBytes.length + "\r\n\r\n";
        }
        return pro200;
    }

    private String get404Protocol(byte[] fileContentBytes) {
        // 拼接404的协议并返回
        return "HTTP/1.1 404\r\n" +
                "Content-Type: text/html;charset=utf-8\r\n" +
                "Content-Language: zh-CN\r\n" +
                "Content-Length: " + fileContentBytes.length + "\r\n\r\n";
    }
}
